package game.module.kefu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.db.DataSourceManager;
import game.module.kefu.bean.QuestionFeedback;

/**
 * 
 * @ClassName: QuestionFeedbackDao
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author admin
 * @date 2017年8月29日
 *
 */
public class QuestionFeedbackDao {

	private static Logger logger = LoggerFactory.getLogger(QuestionFeedbackDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static QuestionFeedbackDao instance = new QuestionFeedbackDao();
	}

	public static QuestionFeedbackDao getInstance() {
		return SingletonHolder.instance;
	}

	private ResultSetHandler<List<QuestionFeedback>> mutiBeanHandler = new ResultSetHandler<List<QuestionFeedback>>() {

		@Override
		public List<QuestionFeedback> handle(ResultSet rs) throws SQLException {
			List<QuestionFeedback> beanList = new ArrayList<QuestionFeedback>();
			while (rs.next()) {
				QuestionFeedback bean = new QuestionFeedback();
				bean.setId(rs.getInt("id"));
				bean.setPlayerId(rs.getInt("player_id"));
				bean.setQuestion(rs.getString("question"));
				bean.setQuestionContent(rs.getString("question_content"));
				bean.setAnswer(rs.getString("answer"));
				bean.setCreateTime(rs.getTimestamp("create_time"));
				bean.setUpdateTime(rs.getTimestamp("update_time"));

				beanList.add(bean);
			}
			return beanList;
		}
	};

	public boolean insert(QuestionFeedback bean) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			int ret = runner.update(
					"insert into question_feedback(player_id,question,question_content,answer,create_time,update_time) values(?,?,?,?,NOW(),?)",
					bean.getPlayerId(),bean.getQuestion(),bean.getQuestionContent(),bean.getAnswer(),bean.getUpdateTime());
			if (ret > 0) {
				addRet = true;
			}
		} catch (SQLException e) {
			logger.error("", e);
		}
		return addRet;
	}

	public List<QuestionFeedback> selectByPlayerId(int playerId) {
		List<QuestionFeedback> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from question_feedback where player_id = ?   order by id desc",
					mutiBeanHandler, playerId);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}
	public List<QuestionFeedback> selectById(int id) {
		List<QuestionFeedback> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from question_feedback where id = ?",
					mutiBeanHandler, id);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public List<QuestionFeedback> selectByPlayerIdstartId(int playerId, int startId) {
		List<QuestionFeedback> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from question_feedback where player_id = ?  and id>? order by id desc", mutiBeanHandler,
					playerId, startId);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public void updateQuestionAnswer(int id, String answer) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			String updateSql = "update question_feedback set answer = ? ,update_time=NOW() where id=?";
			runner.update(updateSql, answer, id);
		} catch (SQLException e) {
			logger.error("", e);
		}
	}


	
	public List<QuestionFeedback> selectLimt(boolean isAnswer) {
		List<QuestionFeedback> ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			if (isAnswer) 
			{
				ret = runner.query("SELECT * from question_feedback where answer IS NOT NULL   order by id desc LIMIT 1000", mutiBeanHandler);
			}else {
				ret = runner.query("SELECT * from question_feedback where 1=1   order by id desc LIMIT 1000", mutiBeanHandler);
			}
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

}
